/** 
 * 
 * Copyright (C) 2010-2012  Nicolas Hernandez
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package fr.univnantes.lina.ftbcleaner;

//import org.w3c.dom.*;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

import fr.univnantes.lina.ftbcleaner.Desambiguate;
import fr.univnantes.lina.ftbcleaner.FTBProcessExtension;
import fr.univnantes.lina.ftbcleaner.FTBXMLVariousDOMHandler;
import fr.univnantes.lina.ftbcleaner.WordForms;
import fr.univnantes.lina.ftbcleaner.WordLemmas;
import fr.univnantes.lina.javautil.DateUtilities;
import fr.univnantes.lina.javautil.XMLUtilities;

import java.io.*;


import java.util.Date;



//import fr.univnantes.lina.uima.connectors.ftb.MultiWordExpression;


/**
 * Extend (and revise ?) the FTB tagged format in particular the w which are parts of multi words expression (mwe) 
 * Build a resource of mwe lemma with arrays of possible values for each mwe features
 * 	 
 * Find values for w partOfMWE
 * 	 Find cat values
 * 	   if exists mw with unambigious values then set 
 * 	   else 
 *       if exists sequence of simple words (sw) with unambigious values then set
 * 	     else 
 *         if exists mw with ambigious values which can be disambiguoused with FB heuristic then set
 * 		   else 
 * 			 if exists sequence of sw with ambigious values which can be disambiguoused with FB heuristic then set
 * 			 else 
 *             if exists sw with unambigious values then set 
 * 			   else 
 * 			     if exists sw with ambigious values which can be disambiguoused with FB heuristic then set
 *               else report cannot be set 
 * 			    
 * 	 Find subcat values (with rules based on mw values)
 * 	    
 * 	 Find others (lemma, gender, number...)
 * 
 * Run with arguments
 * /tmp/domoutfile-light-revised.xml tmp/domoutfile-light-extended.xml
 * /tmp/domoutfile-revised.xml /tmp/domoutfile-extended.xml
 * Check how it deals with amalgame
 * <w cat="P" ee="P" ei="P" lemma="au profit de">
        <w catint="P">au</w>
        <w catint="D"/>
        <w catint="N">profit</w>
        <w catint="P">du</w>
</w>

 * 
 * <p>
 * good java dom tutorial: http://www.exampledepot.com/egs/org.w3c.dom/pkg.html 
 * </p>
 * 
 * @author Nicolas Hernandez
 */
public class FTBProcessExtension extends Desambiguate {

	private String infileName = "";
	private String outfileName = "";
	private Document domDoc = null;



	/*
	 * MAIN 
	 */

	/**
	 * 
	 */
	public static void usage () {
		System.err.println("Usage: java programName <source> <target>");
	}

	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		if (args.length != 2) {
			usage();
			System.err.println("Wrong number of parameter");
			return;}

		Date currentDate = DateUtilities.getNow();
		Date startDate = currentDate;
		System.err.println("Info: L> Start processing "+ DateUtilities.stringFormatADate(currentDate));
		System.err.println("Info: -------------------------------------------------------------------------");

		FTBProcessExtension aFTBExtender = new FTBProcessExtension();

		aFTBExtender.infileName = args[0];
		aFTBExtender.outfileName = args[1];
		
		File inputFile = new File(aFTBExtender.infileName);
		if (!inputFile.exists()) {
			String errorMsg =    "The input file,  " + aFTBExtender.infileName + ", is not valid." ;
			throw new FileNotFoundException  (errorMsg); 
			//MESSAGE_DIGEST, errorMsg,
			//new Object[]{inputCSVDirString});
		}

		// Import XML File to DOM
		aFTBExtender.domDoc = XMLUtilities.readXMLFile2Dom (aFTBExtender.infileName );

		Date previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Import XML File to DOM "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");


		// Collect MW Properties from MW nodes
		NodeList mWNotAGraphicUnitNodeList  = FTBXMLVariousDOMHandler.getNodeListFromXPathExpression(aFTBExtender.domDoc, 
				FTBXMLVariousDOMHandler.mWNotAGraphicUnitXPathExpression);
		//for(int i=0; (i < nodeList.getLength()); i++) displayDomTree(nodeList.item(i));
		WordLemmas aMultiWordLemmas = new WordLemmas();
		aMultiWordLemmas.setMultiWordLemmaMap(aFTBExtender.collectPropertiesFromMultiWordNodeList(mWNotAGraphicUnitNodeList));

		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Collect MW Properties from MW nodes "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");


		// Compile MW pass1
		aFTBExtender.compilePropertiesFromMultiWords(aMultiWordLemmas);

		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Compile MW pass1 "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");

		
		// Collect SW Properties from SW nodes
		WordForms aWordForms = new WordForms();
		NodeList singleWordNodeList  = FTBXMLVariousDOMHandler.getNodeListFromXPathExpression(aFTBExtender.domDoc, 
				FTBXMLVariousDOMHandler.swNotAmalgameXPathExpression);
		aFTBExtender.countWordAttributeValues (
				singleWordNodeList, 
				aWordForms);
		
		//List<WordAttributes> aWordAttributesList = aFTBExtender.collectPropertiesFromSimpleWordNodesSax (
		//		 inputFile, 
		//		 aWordForms);
		
		//System.err.println("Debug: aWordAttributesList.size() "+aWordAttributesList.size());

		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Collect SW Properties from SW nodes "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");

		
		// Search Single Word Sequences Of MultiWords From Simple Word NodeList
		aFTBExtender.searchSingleWordSequencesOfMultiWordsFromSimpleWordNodeList(
				//aWordAttributesList,
				singleWordNodeList, 
				aMultiWordLemmas.getMultiWordFirstWordMap(), aWordForms);

		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Search Single Word Sequences Of MultiWords "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");

		
		// Compile SW properties
		aFTBExtender.compilePropertiesFromSingleWords(aMultiWordLemmas.getMultiWordLemmaMap(),	aWordForms);
		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Compile SW properties "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");

		
		// Sorting  (multi) word forms
		aFTBExtender.sortWordFormsAttributeValues(aWordForms);
		
		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Sorting (multi) word forms"+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");

		
		// Desambiguate
		aFTBExtender.desambiguateMultiWords(mWNotAGraphicUnitNodeList, aMultiWordLemmas.getMultiWordLemmaMap());

		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Desambiguate "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");

		// Export  DOM 2 XML
		XMLUtilities.writeDOM2XMLFile (aFTBExtender.domDoc,aFTBExtender.outfileName);

		previousDate = currentDate;
		currentDate = DateUtilities.getNow();
		System.err.println("Info: L> Export DOM 2 XML "+ DateUtilities.dateDiff(previousDate,currentDate)+ " ms");

		System.err.println("Info: -------------------------------------------------------------------------");
		System.err.println("Info: L> Total time "+ DateUtilities.dateDiff(startDate,currentDate)+ " ms");
		System.err.println("Info: -------------------------------------------------------------------------");

		//System.out.println("<?xml version=\"" +domDoc.getXmlVersion()+ "\" encoding=\""+domDoc.getXmlEncoding() + "\"?>");
		//display("", domDoc);
	}
}